import Vue from 'vue'
import VueRouter from 'vue-router'
import index from '../components/index.vue'
import indexRecommend from '../components/index/recommend.vue'
import indexRank from '../components/index/rank.vue'
import indexAttention from '../components/index/attention.vue'
import change from '../components/change.vue'

import write from '../components/write.vue'
import detail from '../components/detail.vue'

import storage from '../utils/storage.js'

Vue.use(VueRouter)

const routes = [{
		path: '/index',
		components: {
			"index": index
		},
		redirect: '/index/indexRecommend',
		children: [{
				path: "",
				redirect: 'indexRecommend',
			}, {
				path: 'indexRecommend',
				components: {
					'indexLeft': indexRecommend
				}
			},
			{
				path: 'indexRank',
				components: {
					'indexLeft': indexRank
				}
			},
			{
				path: 'indexAttention',
				components: {
					'indexLeft': indexAttention
				}
			}
		]
	},
	{
		path: '/',
		redirect: '/index/indexRecommend'
	},
	{
		path: '/write',
		components: {
			'index': write
		}
	},
	{
		path: '/change',
		components: {
			'index': change
		}
	},
	{
		path: '/detail',
		components: {
			'index': detail
		},
		props: {
			'index': router => ({
				info: router.query.info
			})
		},
	}
]

const router = new VueRouter({
	mode: 'history',
	base: process.env.BASE_URL,
	routes
})

router.beforeEach(function(to, from, next) {
	if (!storage.get('user') && to.path == '/write') {
		alert('您还未登录')
		location.assign(`http://${location.host}/login?type=login`)
	}
	if (!storage.get('user') && to.path == '/') {
		alert('您还未登录')
		location.assign(`http://${location.host}/login?type=login`)
	}
	next()
})

export default router
